/*
 * 深圳市灵智数科有限公司版权所有.
 */
package com.spring.bean.definition;

import com.spring.bean.definition.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 功能说明
 *
 * @author 程健
 * @version 1.0.0
 * @date 2020/12/10
 */
public class BeanAliasDemo {

    public static void main(String[] args) {

        Resource resource = new ClassPathResource("spring-beans.xml");
        //BeanFactory beanFactory =  new XmlBeanFactory(classPathResource);
        //3.1后XmlBeanFactory过期不推荐使用 用ClassPathXmlApplicationContext解析xml或者XmlBeanDefinitionReader解析

        //BeanFactory在启动的时候，不会创建bean的实例，而是在getBean()的时候，才会创建Bean的实例
        BeanFactory beanFactory = new DefaultListableBeanFactory();
        BeanDefinitionReader bdr=new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory);
        bdr.loadBeanDefinitions(resource);

        //取别名的alias bean
        User zhangsan =  (User) beanFactory.getBean("zhangsan");

        System.out.println("userInfo-1"+ zhangsan.toString());

        User zsUserBean = (User) beanFactory.getBean("zsUserBean");

        System.out.println("bean别名是否相等:"+zhangsan.equals(zsUserBean));
    }
}
